﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyThuVien.Business;

namespace QuanLyThuVien.Presentation
{
    public partial class frmQuanLySach : Form
    {
        public frmQuanLySach()
        {
            InitializeComponent();
        }
        SachBUS DSBus = new SachBUS();

        Sach Ds = new Sach();

        DauSachBUS SachBus = new DauSachBUS();

        private void btnNhapMS_Click(object sender, EventArgs e)
        {
            try
            {
                Ds.MaDauSach = cbMaDS.Text;
                Ds.MaSach = txtMaSach.Text;
                DSBus.ThemMaSach(Ds);
                DSBus.LoadLenDG(dgNhapMaSach);
                lbCoMa.Text = DSBus.LaySachCoMa(cbMaDS.Text).ToString();
                lbChuaMa.Text = (int.Parse(lbTong.Text) - int.Parse(lbCoMa.Text)).ToString();
                cbTenSach.Text = "";
                cbMaDS.Text = "";
                txtMaSach.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "QuanLyThuVien");
            }
        }

        private void btnTaoMoiMS_Click(object sender, EventArgs e)
        {
            cbTenSach.Text = "";
            cbMaDS.Text = "";
            txtMaSach.Text = "";
        }

        private void btnSuaMS_Click(object sender, EventArgs e)
        {

        }

        private void btnXoaMS_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có thực sự muốn xóa thông tin mã sách hay không?", "QuanLyThuVien", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Ds.MaSach = txtMaSach.Text;
                DSBus.XoaMaSach(Ds.MaSach);
                DSBus.LoadLenDG(dgNhapMaSach);
            }
        }

        private void dgNhapMaSach_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int a = e.RowIndex;
            cbTenSach.Text = dgNhapMaSach[0, a].Value.ToString();
            cbMaDS.Text = dgNhapMaSach[1, a].Value.ToString();
            txtMaSach.Text = dgNhapMaSach[2, a].Value.ToString();
        }

        private void frmQuanLyDauSach_Load(object sender, EventArgs e)
        {
            DSBus.LoadLenDG(dgNhapMaSach);
            SachBus.LoadDLLenCombobox(cbTenSach); 
            SachBus.LoadDLLenCombobox(cbTenSach);
        }

        private void cbMaDS_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbCoMa.Text = DSBus.LaySachCoMa(cbMaDS.Text).ToString();
            txtMaSach.Text = DSBus.LoadMaSach(cbTenSach.Text, txtMaSach.Text) + (int.Parse(lbCoMa.Text) + 1).ToString();
            lbTong.Text = SachBus.LayTongSach(cbMaDS.Text).ToString();
            lbChuaMa.Text = (int.Parse(lbTong.Text) - int.Parse(lbCoMa.Text)).ToString();
        }

        private void cbTenSach_SelectedIndexChanged(object sender, EventArgs e)
        {
            SachBus.LayMaDSLenCB(cbMaDS, cbTenSach.Text);
        }

    }
}
